<?php

namespace Admins\Controller;
use Common\Controller\AdminBase;
use Admins\Lib\Admin;

class OctoberController extends AdminBase {

    public function index(){
        if (IS_POST) {
            $this->redirect('allworks', $_POST);
        }
        $order=I('get.order','workid');
        //查询条件
        $where = array();
        if(isset($_GET['status']) && $_GET['status']==0)
            $status=0;
        else
            $status = I('get.status',1,'intval');
        $where['status'] = array('EQ', $status);
        $this->assign('statuss', $status);

        //搜索
        $search = I('get.search');
        if (!empty($search)) {
            $this->assign("search", $search);
            //添加开始时间
            $start_time = I('get.start_time');
            if (!empty($start_time)) {
                $start_time = strtotime($start_time);
                $where['create_time'] = array("EGT", $start_time);
                $this->assign('start_time', $start_time);
            }
            //添加结束时间
            $end_time = I('get.end_time');
            if (!empty($end_time)) {
                $end_time = strtotime($end_time);
                $where['create_time'] = array("ELT", $end_time);
                $this->assign('end_time', $end_time);
            }
            if ($end_time > 0 && $start_time > 0) {
                $where['create_time'] = array(array('EGT', $start_time), array('ELT', $end_time));
            }
            //类别
            $type = I('get.type', 0, 'intval');
            if(!empty($type)){
                $this->assign("type", $type);
                $where["type"] = array("EQ", (int) $type);
            }
            //搜索字段
            $searchtype = I('get.searchtype', null, 'intval');
            //搜索关键字
            $keyword = \Input::getVar(I('get.keyword'));
            if (!empty($keyword)) {
                $this->assign("searchtype", $searchtype);
                $this->assign("keyword", $keyword);
                if ($searchtype ==1) {
                    $where['title'] = array("LIKE", "%{$keyword}%");
                }elseif ($searchtype==2) {
                    $userid=M('User')->where(array('nickname'=>$keyword))->getField('userid');
                    $where['userid']=array('EQ',(int) $userid);
                } elseif ($searchtype == 3) {
                    $where["workid"] = array("EQ", (int) $keyword);
                }
            }

        }
        $where['activity'] = 1;
        $model=M('Works');
        //信息总数
        $count = $model->where($where)->count();
        $page = $this->page($count, 30);
        $data = $model->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(''.$order.' DESC')->select();
     
        $this->assign('Page', $page->show())
            ->assign('cateid', $this->cateid)
            ->assign('count', $count)
            ->assign('data', $data);
        $this->display();
    }

}